package com.bobo.controller.back.admin;

import com.bobo.annotations.RequestBody;
import com.bobo.annotations.RequestMapping;
import com.bobo.annotations.RequestMethod;
import com.bobo.controller.base.BaseServlet;
import com.bobo.model.Admin;
import com.bobo.service.admin.AdminServiceImpl;
import com.bobo.service.admin.IAdminService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by bobo on 16-10-27.
 */
@WebServlet(name = "AdminController", urlPatterns = {"/admin/*"})
public class AdminController extends BaseServlet {

    IAdminService adminService = new AdminServiceImpl();

    @RequestMapping("/add")
    public String add1(HttpServletRequest request) {
        Admin admin = (Admin) request.getSession().getAttribute("admin");
        if (admin == null || admin.getLevel() != 0) {
            return "redirect:/back";
        } else {
            return "jsp/back/admin/add";
        }
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(HttpServletRequest request, HttpServletResponse response) {
        String name = request.getParameter("name");
        String pwd = request.getParameter("pwd");
        String pwd1 = request.getParameter("pwd1");
        int level = Integer.parseInt(request.getParameter("level"));
        if(!pwd.equals(pwd1)){
            return "fail";
        }

        Admin admin = new Admin();
        admin.setName(name);
        admin.setPassword(pwd);
        admin.setLevel(level);
        adminService.create(admin);
        return "redirect:/admin/getAdmins";
    }


    @RequestMapping("/del")
    public String del(HttpServletRequest request, HttpServletResponse response) {
        int id = Integer.parseInt(request.getParameter("id"));
        adminService.del(id);
        return "redirect:/admin/getAdmins";
    }

    @RequestMapping(value = "/upd", method = RequestMethod.POST)
    public String upd(HttpServletRequest request, HttpServletResponse response) {
        int id = Integer.parseInt(request.getParameter("id"));

        String name = request.getParameter("name");
        String old = request.getParameter("old");
        String newpwd1 = request.getParameter("newpwd1");
        String newpwd2 = request.getParameter("newpwd2");
        if (newpwd1.equals(newpwd2)) {
            Admin admin = adminService.adminCompare(name, old);
            if (admin != null && admin.getName().equals(name)) {
                admin.setPassword(newpwd1);
                adminService.upd(admin);

                return  "redirect:/admin/get?id=" + id;
            }
        }
        return "fail";
    }

    @RequestMapping("/upd")
    public String upd(HttpServletRequest request) {
       Object obj = request.getSession().getAttribute("admin");

        if(obj != null) {
            Admin temp = (Admin)obj;
            Admin admin = adminService.getAdmin(temp.getId());
            request.setAttribute("admin", admin);
            return "jsp/back/admin/upd";
        }

        return "jsp/back/temp";

    }

    @RequestMapping("/get")
    public String get(HttpServletRequest request) {
        int id = Integer.parseInt(request.getParameter("id"));
        Admin admin = adminService.getAdmin(id);
        request.setAttribute("admin", admin);
        return "jsp/back/admin/get";
    }

    @RequestMapping("/getAdmins")
    public String getAdmins(HttpServletRequest request) {
        Admin admin = (Admin) request.getSession().getAttribute("admin");
        if (admin == null || admin.getLevel() != 0) {
            return "redirect:/back";
        } else {
            List<Admin> admins = adminService.getAdmins();
            request.setAttribute("admins", admins);
            return "jsp/back/admin/getAdmins";
        }
    }





    @RequestMapping("/back")
    public String welcome() {

        return "jsp/back/welcome";
    }


    @RequestMapping("/logout")
    public String logout(HttpServletRequest request) {

        request.getSession().setAttribute("admin", null);

        return "redirect:/admin/login";
    }

}
